package com.semlab.client.widgets;

import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.TextBox;

public class NumberTextBox extends TextBox {

	private String previousValue;

	public NumberTextBox() {
		addKeyUpHandler(new KeyUpHandler() {

			public void onKeyUp(KeyUpEvent event) {
				String input = getText();
				if (input.isEmpty()) {
					previousValue = "";
				} else if (!input.matches("[0-9]*")) {
					setText(previousValue);
					return;
				} else {
					previousValue = input;
				}
			}
		});
	}
	public int getInt(){
		return Integer.parseInt(getText());
	}

}
